package com.jin.flashsaled.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jin.flashsaled.RabbitMQ.RabbitSend;
import com.jin.flashsaled.entity.Order;
import com.jin.flashsaled.entity.SeckillGoods;
import com.jin.flashsaled.entity.SeckillOrder;
import com.jin.flashsaled.entity.User;
import com.jin.flashsaled.result.BuyMessage;
import com.jin.flashsaled.result.Result;
import com.jin.flashsaled.result.ResultEnum;
import com.jin.flashsaled.service.impl.GoodsServiceImpl;
import com.jin.flashsaled.service.impl.OrderServiceImpl;
import com.jin.flashsaled.service.impl.SeckillGoodsServiceImpl;
import com.jin.flashsaled.service.impl.SeckillOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 靳鹏宇
 * @since 2025-09-18
 */
@Slf4j
@Controller
@RequestMapping("/seckill-goods")
public class SeckillGoodsController {
    @Autowired
    SeckillGoodsServiceImpl seckillGoodsService;
    @Autowired
    SeckillOrderServiceImpl seckillOrderService;

    @Autowired
    GoodsServiceImpl goodsService;
    @Autowired
    RabbitSend rabbitSend;


    @PostMapping("/semiskilled")
    public String seckillGoods(User user, Model model, Long goodsId)
    {

        //判断用户是否登录
        if (user==null)
        {
            return "login";
        }
        model.addAttribute("user", user);
        //判断是否有库存
        if(!seckillGoodsService.haveStock(goodsId))
        {
            model.addAttribute("error", ResultEnum.NOSTOCK.getMsg());
            model.addAttribute("user",user);
            model.addAttribute("goods",goodsService.getGoodsWithDetailById(goodsId));
            return "goodDetail";
        }
        //判断用户是否已经购买
        if(seckillOrderService.haveBUy(user.getId(),goodsId)!=null)
        {
            model.addAttribute("error",ResultEnum.HAVEBUY.getMsg());
            model.addAttribute("user",user);
            model.addAttribute("goods",goodsService.getGoodsWithDetailById(goodsId));
            return "goodDetail";
        }

        //直接返回排队中，异步购买，后续轮询结果
        BuyMessage buyMessage = new BuyMessage();
        buyMessage.setUserID(user.getId());
        buyMessage.setGoodID(goodsId);
        rabbitSend.send(buyMessage);

        model.addAttribute("error",ResultEnum.BUYING.getMsg());
        model.addAttribute("user",user);
        model.addAttribute("goods",goodsService.getGoodsWithDetailById(goodsId));
        return "goodDetail";





//        //购买
//        Order order=null;
//        //避免因为异常无法返回商品详情页
//        try {
//            order=orderService.buy(user,goodsId);
//        }
//        catch (Exception e){}
//        if(order==null)
//        {
//            model.addAttribute("error",ResultEnum.ERROR.getMsg());
//            model.addAttribute("user",user);
//            model.addAttribute("goods",goodsService.getGoodsWithDetailById(goodsId));
//            return "goodDetail";
//        }
//        model.addAttribute("order",order);
//
//        return "orderDetail";


    }

    @PostMapping("/getResult")
    public String result(User user, Model model, Long goodsId)
    {
        return seckillGoodsService.getResult(user,model,goodsId);
    }


}
